package com.dapeng.cleardatabackend.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import com.dapeng.cleardatabackend.model.entity.User;
import com.dapeng.cleardatabackend.model.enums.UserGenderEnum;
import com.dapeng.cleardatabackend.model.enums.UserRoleEnum;
import com.dapeng.cleardatabackend.model.enums.UserStatusEnum;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;

/**
 * 用户服务测试
 *
 * @author dapeng
 */
@SpringBootTest
class UserServiceTest {

    @Resource
    private UserService userService;

    private ExecutorService executorService = new ThreadPoolExecutor(50,50,60, TimeUnit.MINUTES, new ArrayBlockingQueue<>(5000));
    public void testAddUser() {

        int batchSize = 20000;
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < 3000000; i+=batchSize) {
            ArrayList<User> userArrayList = new ArrayList<>();
            for (int j = 0; j < batchSize; j++) {
                User user = new User();
                user.setUserAccount(String.format("test%d", i + j));
                user.setUserPassword("12345678");
                user.setUnionId(String.format("test%d", i + j));
                user.setMpOpenId("123456789");
                user.setUsername("测试");
                user.setPhone("12345678910");
                user.setEmail("12345678910@qq.com");
                user.setUserAvatar("https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg");
                user.setUserProfile("用户简介");
                user.setGender(UserGenderEnum.MALE);
                user.setUserRole(UserRoleEnum.USER);
                user.setUserStatus(UserStatusEnum.NORMAL);
                userArrayList.add(user);
            }
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                System.out.println("thread id: " + Thread.currentThread().getName());
                userService.saveBatch(userArrayList, batchSize);
            }, executorService);
            futureList.add(future);
        }
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());
    }

//    @Test
//    void userRegister() {
//        String userAccount = "";
//        String userPassword = "12345678";
//        String checkPassword = "12345678";
//        long result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng";
//        userPassword = "";
//        checkPassword = "12345678";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng";
//        userPassword = "12345678";
//        checkPassword = "";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "da";
//        userPassword = "12345678";
//        checkPassword = "12345678";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng";
//        userPassword = "123456";
//        checkPassword = "123456";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng";
//        userPassword = "12345678";
//        checkPassword = "12345679";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "da peng";
//        userPassword = "12345678";
//        checkPassword = "12345678";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng";
//        userPassword = "12345678";
//        checkPassword = "12345678";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertEquals(-1, result);
//
//        userAccount = "dapeng1";
//        userPassword = "12345678";
//        checkPassword = "12345678";
//        result = userService.userRegister(userAccount, userPassword, checkPassword);
//        Assertions.assertTrue(result > 0);
//    }
}